package com.oxygen.pt.controller;

import cn.hutool.db.PageResult;
import com.oxygen.pt.pojo.entity.ChamberCheckWeekly;
import com.oxygen.pt.pojo.vo.ChamberCheckWeeklyVO;
import com.oxygen.pt.pojo.vo.Result;
import com.oxygen.pt.service.ChamberCheckWeeklyService;
import jakarta.annotation.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 氧舱周巡检报告 Controller
 *
 * @author zhKai
 * @since 2025-09-02
 */
@RestController
@RequestMapping("/chamber-check")
public class ChamberCheckWeeklyServiceController {

    @Resource
    private ChamberCheckWeeklyService chamberCheckWeeklyService;

    /* -------------------- 增 -------------------- */
    @PostMapping
    public Result<Void> add(@RequestBody ChamberCheckWeekly record) {
        int rows = chamberCheckWeeklyService.save(record);
        return rows > 0 ? Result.success() : Result.error("新增失败");
    }

    /* -------------------- 删 -------------------- */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        int rows = chamberCheckWeeklyService.remove(id);
        return rows > 0 ? Result.success() : Result.error("删除失败");
    }

    /* -------------------- 改 -------------------- */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id,
                               @RequestBody ChamberCheckWeekly record) {
        record.setId(id);
        int rows = chamberCheckWeeklyService.update(record);
        return rows > 0 ? Result.success() : Result.error("更新失败");
    }

    /* -------------------- 查（单条） -------------------- */
    @GetMapping("/{id}")
    public Result<ChamberCheckWeekly> detail(@PathVariable Long id) {
        ChamberCheckWeekly data = chamberCheckWeeklyService.get(id);
        return data != null ? Result.success(data) : Result.error("记录不存在");
    }

    /* -------------------- 查（分页） -------------------- */
    /* 分页查询：氧舱号必填 （且为String类型）*/
    @GetMapping("/page")
    public Result<PageResult<ChamberCheckWeeklyVO>> page(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam String chamberNo,
            @RequestParam(required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate beginDate,
            @RequestParam(required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {

        PageResult<ChamberCheckWeeklyVO> page =
                chamberCheckWeeklyService.page(chamberNo, beginDate, endDate, current, size);
        return Result.success(page);
    }
}